Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Use Patch instead of Update to add/remove finalizers #1801

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Baarsgaard
Copy link
Contributor

@Baarsgaard Baarsgaard commented Dec 18, 2024

Managed to solve the perpetual drift noted in #1776 that is rooted in us applying finalizers and updating unintended fields on CRs.

I first attempted to use JSONPatch, but I could not for the life of me get the libraries to comply, despite handcrafting a working patches with kubectl.
MergePatch worked immediately.

Currently, the implementation is hardcoded to only support arrays of one finalizer. More work can be done to make this support multiple if the need ever arises.

@Baarsgaard Baarsgaard marked this pull request as ready for review December 18, 2024 11:45
@Baarsgaard Baarsgaard force-pushed the fix_finalizer_cfg_drift branch from 172cf54 to 1fa382c Compare December 20, 2024 23:49
@Baarsgaard Baarsgaard changed the title Refactor(Internal): Use Patch instead of Update to set/remove finalizers Fix: Use Patch instead of Update to add/remove finalizers Dec 20, 2024
// Remove finalizer through a MergePatch
// Avoids updating the entire object and only changes the finalizers
func removeFinalizer(ctx context.Context, cl client.Client, cr client.Object) error {
patch := []byte(`{"metadata":{"finalizers":null}}`)
Copy link
Collaborator

@weisdd weisdd Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's nice that you can fix the drift with these changes, I would disagree with the way how patches are formed. - Kubernetes objects may have multiple finalizers, whereas, in this PR, there is an assumption that there will always be a single finalizer (owned by the grafana-operator). - Some users might have their own finalizers (think of custom operators).

Since controllerutil has two functions that safely add (AddFinalizer) / remove (RemoveFinalizer ) a particular finalizer, you can, for instance, have them modify finalizers in a CR object and then prepare a patch by copying value from metadata.finalizers.

Copy link
Contributor Author

@Baarsgaard Baarsgaard Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll try and whip up a solution that safely patches the finalizers.

Copy link
Contributor Author

@Baarsgaard Baarsgaard Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up almost copying the controllerUtil.RemoveFinalizer implementation for removing a finalizer as it worked quite well already.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should copy code from anywhere. Do you see any benefits compared to direct calls to their library? - Like it was mentioned in my previous comment, I thought of rather calling controllerutil.AddFinalizer / controllerutil.RemoveFinalizer and then copying finalizers field from the result. - It's pretty straightforward, we don't have to maintain code of the underlying functions, and it doesn't create licensing complexity (I think you have to mention authors when copying Apache2 code).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I partially misunderstood your previous message.
This is iteration aligns with your suggestions!

@Baarsgaard Baarsgaard force-pushed the fix_finalizer_cfg_drift branch 2 times, most recently from cdde804 to b778a52 Compare December 27, 2024 23:31
@Baarsgaard Baarsgaard requested a review from weisdd December 27, 2024 23:35
@Baarsgaard
Copy link
Contributor Author

Baarsgaard commented Dec 28, 2024

Once this is merged, I will likely add finalizers to GrafanaFolder before I start refactoring the reconcile loops for all other CRs one by one

@Baarsgaard Baarsgaard force-pushed the fix_finalizer_cfg_drift branch from b778a52 to a99dc26 Compare December 28, 2024 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants